package com.hula.onion.book.server.spider.platforms.tounawang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by work on 2018/1/20.
 */
@Component
public class TounaSpiderHelper {
    public Map<String, Object> installRequestParams(LoginForm loginForm) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("method", "login");
        map.put("username", loginForm.getUserName());
        map.put("md5Pwd", DigestUtils.md5Hex(loginForm.getPassword()));
        map.put("loginUserType", "0");
        map.put("subtime", String.valueOf(System.currentTimeMillis()));
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param accountStr
     * @return
     */
    public CommonResponse parseAccountBalance(String accountStr) {
        if (StringUtils.isEmpty(accountStr)) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        JSONObject accountJson = (JSONObject) JSONObject.parse(accountStr);
        if ("200".equals(accountJson.getString("status"))) {
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setAmount(accountJson.getJSONObject("result")
                    .getBigDecimal("total"));
            JSONObject waitJson = accountJson.getJSONObject("result")
                    .getJSONObject("wait");
            accountInfo.setWaitIntrest(waitJson.getBigDecimal("total"));
            JSONObject incomJson = accountJson.getJSONObject("result")
                    .getJSONObject("income");
            accountInfo.setTotalIntrest(incomJson.getBigDecimal("totalIncome"));
            return new CommonResponse(accountInfo);
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
